﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLBH.ModelInterface;
using QLBH.DataModel;
using QLBH.Model.Controller;

namespace QLBH.FeatureModel
{
    public class SoNoModel:ISoNoModel
    {
#region properties
        private Guid sonoid;
        private Guid khid;
        private String tenKH;
        private Guid nccid;
        private String tenncc;
        private Decimal sotien;
        private Decimal datra;
        private Guid lydoid;
        private String tenlydo;
        private bool loaino;
        private String sloaino;
        private DateTime ngaytao;
        private DateTime ngaycapnhat;
        private Guid userid;
        private String username;
#endregion
#region Interface member
        public Guid SONOID
        {
            get
            {
                return sonoid;
            }
            set
            {
                sonoid = value;
            }
        }

        public Guid KHID
        {
            get
            {
                return khid;
            }
            set
            {
                khid = value;
            }
        }

        public string TENKH
        {
            get
            {
                return tenKH;
            }
            set
            {
                tenKH = value;
            }
        }

        public Guid NCCID
        {
            get
            {
                return nccid;
            }
            set
            {
                nccid = value;
            }
        }

        public string TENNCC
        {
            get
            {
                return tenncc;
            }
            set
            {
                tenncc = value;
            }
        }

        public decimal SOTIEN
        {
            get
            {
                return sotien;
            }
            set
            {
                sotien = value;
            }
        }

        public Decimal DATRA
        {
            set
            {
                datra = value;
            }
            get
            {
                return datra;
            }
        }

        public Guid LYDOID
        {
            get
            {
                return lydoid;
            }
            set
            {
                lydoid = value;
            }
        }

        public string TENLYDO
        {
            get
            {
                return tenlydo;
            }
            set
            {
                tenlydo = value;
            }
        }

        public bool LOAINO
        {
            get
            {
                return loaino;
            }
            set
            {
                loaino = value;
            }
        }        

        public DateTime NGAYTAO
        {
            get
            {
                return ngaytao;
            }
            set
            {
                ngaytao = value;
            }
        }

        public DateTime NGAYCAPNHAT
        {
            get
            {
                return ngaycapnhat;
            }
            set
            {
                ngaycapnhat = value;
            }
        }

        public Guid USERID
        {
            get
            {
                return userid;
            }
            set
            {
                userid = value;
            }
        }

        public string USERNAME
        {
            get
            {
                return username;
            }
            set
            {
                username = value;
            }
        }
#endregion
       
#region Interface method
        public void InitFromEntity(SONO entity)
        {
            SONOID = entity.SONOID;
            KHID = (Guid)entity.KHID;
            NCCID = (Guid)entity.NCCID;
            SOTIEN = (Decimal)entity.SOTIEN;
            DATRA = (Decimal)entity.DATRA;
            LOAINO = (bool)entity.LOAINO;
            LYDOID = (Guid)entity.LYDOID;
            NGAYTAO = (DateTime)entity.NGAYTAO;
            NGAYCAPNHAT = (DateTime)entity.NGAYCAPNHAT;
            USERID = (Guid)entity.NGUOITAOID;

            //get Khach Hang name
            if (Guid.Empty != khid && null !=khid)
            {
                KHACHHANG khn = KhachHangController.GetInstance().FindBy(kh => kh.KHID == KHID).FirstOrDefault();
                if (khn!=null)
                {
                    TENKH = khn.TENKH;

                    if (loaino)
                    {
                        //KH No
                        sloaino = "KH Nợ";
                    }
                    else
                    {
                        sloaino = "Nợ KH";
                    }
                }
                
            }

            //Get NCC Name
            if (nccid!=null && Guid.Empty!=nccid)
            {
                NHACUNGCAP nccn = NCCController.GetInstance().FindBy(ncc => ncc.NCCUID == nccid).FirstOrDefault();
                if (nccn!=null)
                {
                    TENNCC = nccn.TENNCC;

                    if (loaino)
                    {
                        //NCC No
                        sloaino = "NCC Nợ";
                    }
                    else
                    {
                        sloaino = "Nợ NCC";
                    }
                }
            }

            //Get Ten Ly do
            LYDO lydos = LyDoController.GetInstance().FindBy(ld => ld.ID == lydoid).FirstOrDefault();
            if (lydos!=null)
            {
                TENLYDO = lydos.TEN;
            }

            //Get User Name
            NHANVIEN nvn = NhanVienController.GetInstance().FindBy(nv => nv.NVID == userid).FirstOrDefault();
            if (nvn!=null)
            {
                USERNAME = nvn.TENNV;
            }
        }

        public DataModel.SONO ToEntity()
        {
            SONO entity         = new SONO();
            entity.SONOID       = SONOID;
            entity.NCCID        = NCCID;
            entity.KHID         = KHID;
            entity.SOTIEN       = SOTIEN;
            entity.DATRA        = datra;
            entity.LOAINO       = LOAINO;
            entity.NGAYTAO      = NGAYTAO;
            entity.NGAYCAPNHAT  = NGAYCAPNHAT;
            entity.LYDOID       = LYDOID;
            entity.NGUOITAOID   = USERID;

            return entity;
        }
#endregion
        
    }
}
